package events;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import db.Blog;
import db.Events;
import db.SessionFactoryUtil;

public class EventsHome {
	
	/**
	 * Pobranie eventow
	 * @return List articles
	 */
	public static List getEvents() {
		Session session = SessionFactoryUtil.getSession();
		
		Query query = session.createQuery("from Events");
		List list = query.list();
		if(list.isEmpty()) {
			return null;
		} else {
			return list;			
		}	
	}

	/**
	 * Edycja eventu
	 */
	
	public static int editEvent(Events event, int idE) {
		Session session = SessionFactoryUtil.getSession();

		Query query = session.createQuery("from Events where idE = :idE");
		query.setParameter("idE", idE);
		List list = query.list();
		Events ev = (Events) list.get(0);
		
		ev.setStart(event.getStart());
		ev.setStop(event.getStop());
		ev.setTitle(event.getTitle());
		ev.setContents(event.getContents());
		ev.setDistance(event.getDistance());
		ev.setData(event.getData());

		session.update(ev);
		
		return 1;
	}

	/**
	 * Usuniecie eventu
	 */
	
	public static int removeEvent(int idE) {
		Session session = SessionFactoryUtil.getSession();
		
		Query query = session.createQuery("delete from Events where idE = :idE");
		query.setParameter("idE", idE);
		int result = query.executeUpdate();
		return result;
	}
	
	
	/**
	 * Pobranie artykulu
	 */
	
	public static Events getEvent(int idE) {
		Session session = SessionFactoryUtil.getSession();
		
		Query query = session.createQuery("from Events where idE = :idE");
		query.setParameter("idE", idE);
		List list = query.list();
		if(list.isEmpty()) {
			return null;
		} else {
			return (Events) list.get(0);			
		}	
	}

}
